package de.htwg_konstanz.infen.aufg3;

import de.htwg_konstanz.infen.aufg3.inputXML.jaxbGenerated.TOccurrence;

public class CustomHashMapValueItem {

	public final String PATH;
	public final String ELEMENTNAME;
	private TOccurrence OCCURENCE = TOccurrence.ONE;
	private int MIN;
	private int MAX;
	private int COUNTER = 0;

	public void setOccurence(TOccurrence to)
	{
		OCCURENCE = to;
	}
	
	public CustomHashMapValueItem(String _path) {
		PATH = _path;
		ELEMENTNAME = seperateElementNameFromPath(PATH);
		MIN = 1;
		MAX = 1;
		refreshOccurenceStatus();
	}

	public CustomHashMapValueItem(String _path, int _min, int _max) {
		PATH = _path;
		ELEMENTNAME = seperateElementNameFromPath(PATH);
		MIN = _min;
		MAX = _max;
		refreshOccurenceStatus();
	}

	public int getMIN() {
		return MIN;
	}

	public void setMIN(int _min) {
		MIN = _min;
		refreshOccurenceStatus();
	}

	public int getMAX() {
		return MAX;
	}

	public void setMAX(int _max) {
		MAX = _max;
		refreshOccurenceStatus();
	}

	public int getCOUNTER() {
		return COUNTER;
	}

	public TOccurrence occurence() {
		return OCCURENCE;
	}

	private void refreshOccurenceStatus() {
		if (this.MIN == 1 && this.MAX == 1)
			OCCURENCE = TOccurrence.ONE;
		else if (this.MIN >= 1 && this.MAX > 1)
			OCCURENCE = TOccurrence.ONE_OR_MANY;
		else if (this.MIN < 1 && this.MAX > 1)
			OCCURENCE = TOccurrence.ZERO_OR_MANY;
		else if (this.MIN < 1 && this.MAX == 1)
			OCCURENCE = TOccurrence.ZERO_OR_ONE;
		else if (this.MIN > 1)
			OCCURENCE = TOccurrence.ONE_OR_MANY;
		else
			OCCURENCE = TOccurrence.ONE;
	}

	private static String seperateElementNameFromPath(String _path) {
		String[] splittedPath = _path
				.split("" + DataGuideHelper.itemSeperator);
		return splittedPath[splittedPath.length - 1];
	}

	public void increment() {
		COUNTER++;
		refreshOccurenceStatus();
	}

	public void increment(int _value) {
		COUNTER = COUNTER + _value;
		refreshOccurenceStatus();
	}

	public void decrement() {
		COUNTER--;
		refreshOccurenceStatus();
	}

	public void decrement(int _value) {
		COUNTER = COUNTER - _value;
		refreshOccurenceStatus();
	}

	public void resetCounter(){
		COUNTER = 0;
		refreshOccurenceStatus();
	}
	
	@Override
	public String toString() {
		return OCCURENCE.toString();
	}
}
